
    //
//  DF_DrawerViewController.m
//  DF_DrawerDemo
//
//  Created by 董华昊 on 16/6/10.
//  Copyright © 2016年 董华昊. All rights reserved.
//

#import "DF_DrawerViewController.h"
#define  screenW [UIScreen mainScreen].bounds.size.width
@interface DF_DrawerViewController ()

@end

@implementation DF_DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpAllChild];
    [self touchMove];
}
//  创建手势
-(void)touchMove
{

//    添加拖动的手势
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [_mainView addGestureRecognizer:pan];
//    添加点按的手势
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [self.view addGestureRecognizer:tap];
}
#define targetR 300
#define targetL -200
//让mainV拖动的时候调用
-(void)pan :(UIPanGestureRecognizer *)pan
{
//    获得X轴的偏移量
    CGFloat offsetX=[pan translationInView:_mainView].x;
//    修改mainV的frame
    _mainView.frame=[self frameWithOffsetX:offsetX];
//    复位
    [pan setTranslation:CGPointZero inView:_mainView];
    [self isShowLeftView];
    if (pan.state==UIGestureRecognizerStateEnded)
    {
//    需要定位
    CGFloat target=0;
    if (_mainView.frame.origin.x>screenW*0.5)
        {
//        main.x>screenW*0.5 定位到右边的某一点
        target=targetR;
        }else if (CGRectGetMaxX(_mainView.frame)<screenW*0.5){
//        max(main.x)<screenW*0.5定位在左边的某一点
        target=targetL;
        
        }
//    计算偏移量
    CGFloat offsetX=target=_mainView.frame.origin.x;
    [UIView animateWithDuration:0.25 animations:^{
        _mainView.frame=[self frameWithOffsetX:offsetX];
    }];
  }
}
//显示左视图
-(void)isShowLeftView
{
//    判断当前显示的是左视图还是右视图
    if (_mainView.frame.origin.x>0)
        {//往右边，显示的是左视图；
//        右边开启隐藏
        _rightView.hidden=YES;
        }else if (_mainView.frame.origin.x<0)
        {//往左边，显示的是右视图；
//        右边关闭隐藏
            _rightView.hidden=NO;
        }
}
//当点击View的时候调用
-(void)tap
{
    if (_mainView.frame.origin.x != 0) {
        [UIView animateWithDuration:0.25 animations:^{
            _mainView.frame=self.view.bounds;
        }];
    }
}
#define maxY 100
-(CGRect)frameWithOffsetX:(CGFloat)offsetX
{
    CGFloat screenH=[UIScreen mainScreen].bounds.size.height;
    CGRect frame=_mainView.frame;
//    最新的X；
    CGFloat X=frame.origin.x +=offsetX;
//    最新的Y；
    CGFloat Y=fabs(X/screenW*maxY);
//    最新的H；
    CGFloat H=screenH-2*Y;
//    最新的W；
    CGFloat scale=H/screenH;
    CGFloat W=screenW*scale;
    
//    返回最新的frame值
    return  CGRectMake(X, Y, W,H);


}
-(void)setUpAllChild
{
//  创建左视图
    UIView *leftView=[[UIView alloc]initWithFrame:self.view.bounds];
    _leftView=leftView;
    [self.view addSubview:leftView];
//  创建右视图
    UIView *rightView=[[UIView alloc]initWithFrame:self.view.bounds];
    _rightView=rightView;
    [self.view addSubview:rightView];
//  创建主视图
    UIView *mainView=[[UIView alloc]initWithFrame:self.view.bounds];
    _mainView=mainView;
    [self.view addSubview:mainView];


}


@end
